<?php

namespace App\Http\Controllers\Api\v2;

use App\Http\Controllers\ApiController;
use App\Models\Item;
use App\Models\Shop;
use Illuminate\Support\Facades\DB;

class ShopController extends ApiController
{
    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getShop(){

        $shop_id = $this->request->input('shop_id');
        $shop = Shop::find($shop_id);
        if ($shop) {
            $shop->logo = image_url($shop->logo);
            $shop->image = image_url($shop->image);
            $shop->item_count = $shop->items()->count();
            if ($shop->item_count > 10000) {
                $shop->item_count = round($shop->item_count/10000, 2).'万';
            }
            if ($shop->total_sold > 10000) {
                $shop->total_sold = round($shop->total_sold/10000, 2).'万';
            }
            return ajaxReturn(['shop'=>$shop]);
        }else {
            return ajaxError(404, 'not found');
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getShopList(){

        $offset = $this->request->input('offset');
        $count  = $this->request->input('count');
        $offset = $offset ? $offset : 0;
        $count  = $count ? $count : 20;

        $lng = $lat = 0;
        $location = $this->request->input('location');
        if ($location) {
            $point = explode(',', $location);
            $lng = floatval($point[0]);
            $lat = floatval($point[1]);
        } else {
            $lat = floatval($this->request->input('lat'));
            $lng = floatval($this->request->input('lng'));
        }

        $shops = [];
        Shop::where('item_count', '>', 2)->opening()
            ->offset($offset)->limit($count)->orderByDesc('total_sold')->get()
            ->map(function ($shop) use (&$shops, $lat, $lng){
                if ($shop->lng && $shop->lat) {
                    $shop->distance = distance(getDistance($lat, $lng, $shop->lat, $shop->lng));
                }else{
                    $shop->distance = '';
                }

                $shop->logo = image_url($shop->logo);
                if ($shop->total_sold > 10000) {
                    $shop->total_sold = round($shop->total_sold/10000, 2).'万';
                }
                $shop->items = [];
                $shops[$shop->shop_id] = $shop->toArray();
        });

        $shopIds = array_keys($shops);
        $prefix = DB::getTablePrefix();
        $query = DB::table(DB::raw($prefix."item as i"))
            ->where(DB::raw("(select count(*) from {$prefix}item where shop_id=i.shop_id and itemid<i.itemid)"),'<', 3);

        $query->whereIn('shop_id', $shopIds)->orderByDesc('itemid')->get()->map(function ($item) use (&$shops){
            $item->thumb = image_url($item->thumb);
            $item->image = image_url($item->image);
            $shops[$item->shop_id]['items'][] = $item;
        });

        return ajaxReturn(['items'=>array_values($shops)]);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function save(){

        $shop = $this->request->input('shop');
        Shop::where('uid', $this->uid)->update($shop);
        return ajaxReturn(['retrun_code'=>0, 'return_msg'=>'success']);
    }
}
